//
//  DataFMDBHandle.m
//  项目A_百思不得姐
//
//  Created by wudongyang on 16/6/3.
//  Copyright © 2016年 wudongyang. All rights reserved.
//

#import "DataFMDBHandle.h"

@interface DataFMDBHandle ()

@property(nonatomic, strong)FMDatabaseQueue *myQueue;

@end
@implementation DataFMDBHandle

+(DataFMDBHandle *)shareDatHandle{
    static DataFMDBHandle *data = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        data = [[DataFMDBHandle alloc] init];
    });
    return data;
}
// 打开数据库
-(void)openDB{
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    DSLog(@"%@",path);
    NSString *dataPath = [path stringByAppendingPathComponent:@"dataBase.sqlite"];
    self.myQueue = [FMDatabaseQueue databaseQueueWithPath:dataPath];
    [self.myQueue inDatabase:^(FMDatabase *db) {
        BOOL result = [db open];
        if (result) {
            NSLog(@"数据库打开成功");
        }
        else{
            NSLog(@"数据库打开失败");
        }
        
    }];
}
// 创建收藏表
-(void)createCollectTable{
[self.myQueue inDatabase:^(FMDatabase *db) {
    BOOL result = [db executeUpdate:@"create table if not exists collect(number integer primary key autoincrement, title text, userName text, collectId text, Image text, Gif text, Video text, Type text, Url text)"];
    if (result) {
        NSLog(@"创建收藏表成功");
    }else{
        NSLog(@"创建电影表失败");
    }
    
}];

}
// 收藏表插入数据
-(void)insertCollectData:(MineCollectModel *)model withUserName:(NSString *)userName{
[self.myQueue inDatabase:^(FMDatabase *db) {
    NSString *sql = [NSString stringWithFormat:@"insert into collect(title, userName, collectId, Image, Gif, Video, Type, Url) values('%@','%@','%@','%@','%@','%@','%@','%@')",model.title, userName, model.collectId, model.Image, model.Gif, model.Video, model.Type, model.Url];
    BOOL result = [db executeUpdate:sql];
    if (result) {
        NSLog(@"插入成功");
    }else{
        NSLog(@"插入失败");
    }
    
}];

}
// 删除数据
-(void)deleteCollectData:(NSString *)title{
    NSString *sql = [NSString stringWithFormat:@"delete from collect where title = '%@'",title];
    [self.myQueue inDatabase:^(FMDatabase *db) {
        BOOL result = [db executeUpdate:sql];
        if (result) {
            NSLog(@"删除成功");
        }
        else{
            NSLog(@"删除失败");
        }
        
    }];
}
// 查询数据
-(NSMutableArray *)selectAllCollecData:(NSString *)userName withCompare:(BOOL)compare{
    NSMutableArray *arrayOne = [NSMutableArray array];
    NSMutableArray *arrayTwo = [NSMutableArray array];
    [self.myQueue inDatabase:^(FMDatabase *db) {
        NSString *sql = [NSString stringWithFormat:@"select *from collect where userName = '%@'",userName];
        FMResultSet *result = [db executeQuery:sql];
        
        while ([result next]) {
            NSString *title = [result objectForColumnName:@"title"];
            NSString *collectId = [result objectForColumnName:@"collectId"];
            NSString *Image = [result objectForColumnName:@"Image"];
            NSString *Gif = [result objectForColumnName:@"Gif"];
            NSString *Video = [result objectForColumnName:@"Video"];
            NSString *Type = [result objectForColumnName:@"Type"];
            NSString *Url = [result objectForColumnName:@"Url"];
            MineCollectModel *model = [[MineCollectModel alloc] init];
            model.title = title;
            model.collectId = collectId;
            model.Image = Image;
            model.Gif = Gif;
            model.Video = Video;
            model.Type = Type;
            model.Url = Url;
            [arrayOne addObject:model];
            [arrayTwo addObject:model.title];
        }
        
    }];
    if (compare == YES) {
        return arrayOne;
    }else{
        return arrayTwo;
    }
}








     //删除账户信息列表
-(void)dropcollecTable{
         NSString *sql = [NSString stringWithFormat:@"drop table collect"];
    [self.myQueue inDatabase:^(FMDatabase *db) {
        BOOL result = [db executeUpdate:sql];
        if (result) {
            NSLog(@"删除成功");
        }
        else{
            NSLog(@"删除失败");
        }
        
    }];

    
         
}







@end
